import { GAME_CONFIG } from './config.js';

export class Player {
    constructor(scene) {
        this.pos = new THREE.Vector3(
            GAME_CONFIG.PLAYER_START_POS.x,
            GAME_CONFIG.PLAYER_START_POS.y,
            GAME_CONFIG.PLAYER_START_POS.z
        );
        this.vel = new THREE.Vector3(0, 0, 0);
        this.acc = new THREE.Vector3(0, 0, 0);
        this.hit = false;
        this.wantX = 0;
        this.jumping = false;
        this.baseSpeed = GAME_CONFIG.BASE_SPEED;
        this.magnetRange = 5;
        
        // 创建玩家的3D模型
        this.createMesh(scene);
    }

    createMesh(scene) {
        // 创建一个平面几何体作为玩家模型
        const geometry = new THREE.PlaneGeometry(4, 4);
        
        // 加载SVG贴图
        const textureLoader = new THREE.TextureLoader();
        const spriteMaterial = new THREE.SpriteMaterial({
            map: textureLoader.load('assets/images/player.svg'),
            transparent: true,
            side: THREE.DoubleSide
        });
        
        // 创建精灵
        this.mesh = new THREE.Sprite(spriteMaterial);
        this.mesh.scale.set(4, 4, 1);
        this.mesh.position.copy(this.pos);
        
        // 添加到场景
        scene.add(this.mesh);
        
        // 添加阴影
        const shadowGeometry = new THREE.CircleGeometry(1.5, 32);
        const shadowMaterial = new THREE.MeshBasicMaterial({
            color: 0x000000,
            transparent: true,
            opacity: 0.3,
            side: THREE.DoubleSide
        });
        this.shadow = new THREE.Mesh(shadowGeometry, shadowMaterial);
        this.shadow.rotation.x = -Math.PI / 2;
        this.shadow.position.y = 0.1;
        scene.add(this.shadow);
    }

    update(gravity) {
        // 应用重力到加速度
        this.acc.add(gravity);

        // 更新速度和位置
        this.vel.add(this.acc);
        this.pos.add(this.vel);

        // 重置加速度
        this.acc.set(0, 0, 0);

        // 平滑过渡到目标X位置
        if (this.wantX > this.pos.x) this.pos.x++;
        if (this.wantX < this.pos.x) this.pos.x--;

        // 限制玩家移动范围
        this.pos.clamp(
            new THREE.Vector3(-7.5, 2, 10),
            new THREE.Vector3(7.5, 13, 10)
        );

        // 更新模型位置
        if (this.mesh) {
            this.mesh.position.copy(this.pos);
            // 更新阴影位置
            if (this.shadow) {
                this.shadow.position.x = this.pos.x;
                this.shadow.position.z = this.pos.z;
                // 根据玩家高度调整阴影大小和透明度
                const shadowScale = 1 + (this.pos.y - 2) * 0.1;
                const shadowOpacity = 0.3 - (this.pos.y - 2) * 0.02;
                this.shadow.scale.set(shadowScale, shadowScale, 1);
                this.shadow.material.opacity = Math.max(0, shadowOpacity);
            }
        }
    }

    jump(isInverted = false) {
        if (!this.jumping) {
            this.jumping = true;
            this.acc.y += isInverted ? -GAME_CONFIG.JUMP_FORCE : GAME_CONFIG.JUMP_FORCE;
        }
    }

    move(direction, isInverted = false) {
        const actualDirection = isInverted ? -direction : direction;
        if (actualDirection < 0 && this.wantX >= 0) this.wantX -= 7.5;
        if (actualDirection > 0 && this.wantX <= 0) this.wantX += 7.5;
    }

    checkCollision(y, cameraRotation) {
        if ((y >= 13 && cameraRotation !== 0) || (y <= 2 && cameraRotation == 0)) {
            this.jumping = false;
            this.vel.y = 0;
        }
    }

    reset() {
        this.hit = false;
        this.pos.set(
            GAME_CONFIG.PLAYER_START_POS.x,
            GAME_CONFIG.PLAYER_START_POS.y,
            GAME_CONFIG.PLAYER_START_POS.z
        );
        this.vel.set(0, 0, 0);
        this.acc.set(0, 0, 0);
        this.wantX = 0;
        this.jumping = false;
        
        // 重置模型位置
        if (this.mesh) {
            this.mesh.position.copy(this.pos);
        }
    }
} 